/*
 * dla_Mamy.c
 *
 * Created: 2012-05-28 22:31:09
 *  Author: Sebastian_2
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <util/delay.h>
#include "conf.h"
#include "drivers/XMEGA.h"
#include "drivers/ADS7843.h"
#include "drivers/SD/diskio.h"
//#include "drivers/MCP79410.h"
//#include "drivers/TMP431.h"
#include "Globals.h"
#include "Colours.h"
#include "menus/Menus.h"
#include "MP3.h"
#include "misc.h"

/*volatile uint16_t gColour = BACKGROUND;*/
point gP1 = {0, 0};
point gP2 = {0, 0};
volatile uint8_t screenCount = 0;
volatile uint8_t skippedSamples;
/*volatile enum Status gStatus;
volatile enum Gesture gGesture;
volatile enum Keyboard_Display mKeyboardDisplay;*/

//TWI_Master_t gTWI_Master;

//FIL gVS1003File;
//AudioFile_t gVS1003FileInfo;
//uint8_t gVS1003Buffer1[VS1003_BUFSIZE], gVS1003Buffer2[VS1003_BUFSIZE];
//uint16_t gVS1003BufferLenth = 0;

int main(void)
{
#ifndef ATMEGA 
	CLKSYS_Enable(OSC_PLLEN_bm);
	CLKSYS_PLL_Config(OSC_PLLSRC_RC2M_gc, 16);
	while(CLKSYS_IsReady(OSC_PLLRDY_bm) == 0);
	CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_PLL_gc);
	
	TCE1.CTRLA = 0x05;      // Prescaler: clk/64
    TCE1.PER   = 15624;     // 32Hz
    TCE1.INTCTRLA = 0x02;   // Timer overflow is a medium level interrupt
#endif
	
#ifdef ATMEGA
	power_timer0_enable();
	power_timer1_enable();
	power_timer2_enable();
	
	// timer for gesture detection
	TCCR0A = (1 << WGM01); // CTC mode
	TIMSK0 = (1 << OCIE0A); // enable interrupt
	TCNT0 = 0;
	OCR0A = 0xFF;
	
	// timer for FatFs
	TCCR2A = (1 << WGM21); // CTC mode
	TIMSK2 = (1 << OCIE2A);
	TCNT2 = 0;
	OCR2A = 0xC2; // about 10ms
	TCCR2B = (1 << CS22) | (1 << CS21) | (1 << CS20); // start timer, 1024 prescaler
	
	// pseudo RTC
	TIMSK1 |= (1 << OCIE1A);
	TCNT1 = 0;
	OCR1A = 0x4C4A; // about 1s
	TCCR1B |= (1 << WGM12)|(1 << CS12)|(1 << CS10);
#endif
	
	SSD1289_Init();
	SSD1289_Clear();
	//TWI_MasterInit(&gTWI_Master, &GLOBAL_TWI, TWI_MASTER_INTLVL_OFF_gc, GLOBAL_TWI_BAUD);
	//MCP79410_Init();
	//gRTC = MCP79410_Get();
	//TMP431_Init();
	ADS7843_Init();
	//VS1003_Init();
	
	FATFS FileSystemObject;
	if(f_mount(0, &FileSystemObject) != FR_OK)
		SSD1289_PrintString("oh shit\n", &gP1, &droidSans22ptFontInfo);
	
	DSTATUS driveStatus = disk_initialize();
	if((driveStatus & STA_NOINIT) || (driveStatus & STA_NODISK) || (driveStatus & STA_PROTECT))
		SSD1289_PrintString("oh shit2\n", &gP1, &droidSans22ptFontInfo);
	
	//MP3_ScanFiles();
	
	gRTC.hours = 12; gRTC.minutes = 55; gRTC.date = 21; gRTC.month = 7; gRTC.year = 12; gRTC.day = 6;
	gLocalTemperature.integer = 15; gLocalTemperature.fraction = 50;
	gRemoteTemperature.integer = -15; gRemoteTemperature.fraction = 25;
	
	point screen[10]; // for gesture detection
	
	strcpy(gVS1003FileInfo.artistName, "Wybierz");
	strcpy(gVS1003FileInfo.songName, "utwor");
	
	gStatus = TOUCHSCREEN_IDLE | PLAYER_STOP;
	gGesture = GESTURE_NONE;
	mKeyboardDisplay = UPPERCASE;
	
	Menu_Home_Enter();
	
	sei();
    while(1)
    {
		if(gRTCStatus == RTC_READ) {
			// read from external RTC
			gRTC.hours = 0;
			gRTC.date++;
			
			gRTCStatus = 0;
		} else if(gRTCStatus == RTC_SECOND) {
			gRTCStatus = 0;
		} else if(gRTCStatus == RTC_MINUTE) {
			if(gMenu == MENU_MAIN || gMenu == MENU_MP3 || gMenu == MENU_MP3_ARTISTS || gMenu == MENU_RADIO || gMenu == MENU_RADIO_STATIONS) {
				drawInfoBar();
			}
			gRTCStatus = 0;	
		}
		
		if(gStatus & TOUCHSCREEN_READ) {
			if(bit_is_clear(ADS7843_INT_PIN, ADS7843_INT_BIT)) {
				screen[screenCount] = ADS7843_GetSamples();
				screenCount++;
			}
			if(screenCount >= 10 || bit_is_set(ADS7843_INT_PIN, ADS7843_INT_BIT)) {
				TCCR0B = 0;
				ADS7843_DetectGesture(screenCount, screen);
				if(gGesture == GESTURE_POINT) {
					// little calibration
					if(screen[5].x < 160) {
						screen[5].x *= 117;
						screen[5].x /= 100;
						screen[5].x -= 28;
					} else {
						screen[5].x *= 59;
						screen[5].x /= 50; // 59/50 = 118/50
						screen[5].x -= 30;
					}
				}
				screenCount = 0;
				skippedSamples = 0;
			}
			gStatus &= ~TOUCHSCREEN_READ;
			gStatus |= TOUCHSCREEN_IDLE;
		}
		// rest of gStatus ifs come here
		if(gGesture == GESTURE_POINT) {
			if(gMenu == MENU_MAIN) {
				if(isPointInRange(&screen[5], &(point){19, 56}, &(point){75, 112})) {
					Menu_MP3_Enter();
				} else if(isPointInRange(&screen[5], &(point){94, 56}, &(point){150, 112})) {
					Menu_Radio_Enter();
				} else if(isPointInRange(&screen[5], &(point){169, 56}, &(point){225, 112})) {
					Menu_Calendar_Enter();
				} /*else if(isPointInRange(&screen[5], &(point){244, 56}, &(point){300, 112})) {
					Menu_Photo_Albums_Enter();
				}*/
			} else if(gMenu == MENU_MP3) {
				if(isPointInRange(&screen[5], &(point){19, 56}, &(point){75, 112})) {
					Menu_MP3_BackToMain();
				} else if(isPointInRange(&screen[5], &(point){72, 48}, &(point){245, 84})) {
					Menu_MP3_Player_Enter();
				} else if(isPointInRange(&screen[5], &(point){72, 90}, &(point){245, 116})) {
					Menu_MP3_Artists_Enter();
				}
			} else if(gMenu == MENU_MP3_PLAYER) {
				if(isPointInRange(&screen[5], &(point){7, 169}, &(point){63, 225})) {
					Menu_MP3_Player_Previous();
				} else if(isPointInRange(&screen[5], &(point){70, 169}, &(point){126, 225})) {
					Menu_MP3_Player_PlayPause();	
				} else if(isPointInRange(&screen[5], &(point){133, 169}, &(point){189, 225})) {
					Menu_MP3_Player_Stop();
				} else if(isPointInRange(&screen[5], &(point){196, 169}, &(point){252, 225})) {
					Menu_MP3_Player_RepeatToggle();
				} else if(isPointInRange(&screen[5], &(point){259, 169}, &(point){315, 225})) {
					Menu_MP3_Player_Next();
				}
			} else if(gMenu == MENU_MP3_ARTISTS) {
				if(isPointInRange(&screen[5], &(point){10, 28}, &(point){SSD1289_MaxX, 58})) {
					mArtistsCurrentDir = 0;
					Menu_MP3_Songs_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 64}, &(point){SSD1289_MaxX, 94})) {
					mArtistsCurrentDir = 1;
					Menu_MP3_Songs_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 100}, &(point){SSD1289_MaxX, 130})) {
					mArtistsCurrentDir = 2;
					Menu_MP3_Songs_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 136}, &(point){SSD1289_MaxX, 166})) {
					mArtistsCurrentDir = 3;
					Menu_MP3_Songs_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 172}, &(point){SSD1289_MaxX, 202})) {
					mArtistsCurrentDir = 4;
					Menu_MP3_Songs_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 208}, &(point){SSD1289_MaxX, 238})) {
					mArtistsCurrentDir = 5;
					Menu_MP3_Songs_Refresh();
				}
			} else if(gMenu == MENU_MP3_SONGS) {
				if(isPointInRange(&screen[5], &(point){10, 28}, &(point){SSD1289_MaxX, 58})) {
					mSongsSelectedRow = 0;
					mPlayerGoBackToMain = 0;
					Menu_MP3_Player_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 64}, &(point){SSD1289_MaxX, 94})) {
					mSongsSelectedRow = 0;
					mPlayerGoBackToMain = 0;
					Menu_MP3_Player_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 100}, &(point){SSD1289_MaxX, 130})) {
					mSongsSelectedRow = 0;
					mPlayerGoBackToMain = 0;
					Menu_MP3_Player_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 136}, &(point){SSD1289_MaxX, 166})) {
					mSongsSelectedRow = 0;
					mPlayerGoBackToMain = 0;
					Menu_MP3_Player_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 172}, &(point){SSD1289_MaxX, 202})) {
					mSongsSelectedRow = 0;
					mPlayerGoBackToMain = 0;
					Menu_MP3_Player_Refresh();
				} else if(isPointInRange(&screen[5], &(point){10, 208}, &(point){SSD1289_MaxX, 238})) {
					mSongsSelectedRow = 0;
					mPlayerGoBackToMain = 0;
					Menu_MP3_Player_Refresh();
				}
			} else if(gMenu == MENU_RADIO) {
				if(isPointInRange(&screen[5], &(point){94, 56}, &(point){150, 112})) {
					Menu_Radio_BackToMain();
				} else if(isPointInRange(&screen[5], &(point){72, 48}, &(point){245, 84})) {
					Menu_Radio_Stations_Enter();
				} else if(isPointInRange(&screen[5], &(point){72, 90}, &(point){245, 116})) {
					Menu_Radio_TurnOff();
				}
			} else if(gMenu == MENU_RADIO_STATIONS) {
				if(isPointInRange(&screen[5], &(point){10, 28}, &(point){SSD1289_MaxX, 58})) {
					mRStationsSelectedRow = 0;
					// do some I2C magic
					Menu_Main_Enter();
				} else if(isPointInRange(&screen[5], &(point){10, 64}, &(point){SSD1289_MaxX, 94})) {
					mRStationsSelectedRow = 1;
					// do some I2C magic
					Menu_Main_Enter();
				} else if(isPointInRange(&screen[5], &(point){10, 100}, &(point){SSD1289_MaxX, 130})) {
					mRStationsSelectedRow = 2;
					// do some I2C magic
					Menu_Main_Enter();
				} else if(isPointInRange(&screen[5], &(point){10, 136}, &(point){SSD1289_MaxX, 166})) {
					mRStationsSelectedRow = 3;
					// do some I2C magic
					Menu_Main_Enter();
				} else if(isPointInRange(&screen[5], &(point){10, 172}, &(point){SSD1289_MaxX, 202})) {
					mRStationsSelectedRow = 4;
					// do some I2C magic
					Menu_Main_Enter();
				} else if(isPointInRange(&screen[5], &(point){10, 208}, &(point){SSD1289_MaxX, 238})) {
					mRStationsSelectedRow = 5;
					// do some I2C magic
					Menu_Main_Enter();
				}
			} else if(gMenu == MENU_CALENDAR) {
				for(uint8_t i = 0; i < 5; i++) {
					for(uint8_t j = 0; j < 6; j++) {
						point start = {47*j + 4, 38*i + 24};
						point end = {47*(j+1) + 4, 38*(i+1) + 24};
						if(isPointInRange(&screen[5], &start, &end)) {
							mCalendarDayToDisplay = 7*i + (j+1) - (mCalendarFirstDay-1);
							mCalendarSelectedDay = mCalendarDayToDisplay;
							mCalendarSelectedMonth = mCalendarMonthToDisplay;
							mCalendarSelectedYear = mCalendarYearToDisplay;
							mCalendarSelectedHour = 12;
							mCalendarSelectedMinutes = 0;
							mCalendarEventName[0] = 0;
							Menu_Calendar_NewEvent_Add();
							break;
						}
					}
				}
			} else if(gMenu == MENU_KEYBOARD_EVENT) {
				if(mKeyboardDisplay == UPPERCASE) {
					if(isPointInRange(&screen[5], &(point){4, 159}, &(point){32, 190})) {
						mKeyboardDisplay = LOWERCASE;
						Menu_Keyboard_Refresh();
					} else if(isPointInRange(&screen[5], &(point){4, 208}, &(point){32, 233})) {
						mKeyboardDisplay = NUM;
						Menu_Keyboard_Refresh();					
					} /*else if(isPointInRange(&screen[5], &(point){270, 159}, &(point){311, 181})) {
						Menu_Keyboard_RemoveLastChar();
					} else if(isPointInRange(&screen[5], &(point){270, 199}, &(point){311, 227})) {
						// list of most used entries
					} else if(isPointInRange(&screen[5], &(point){103, 194}, &(point){203, 235})) {
						Menu_Keyboard_AddNewChar(' ');
					} else {
						uint16_t letters_str[26] = {15, 47, 87, 112, 140, 166, 192, 222, 245, 276, 32, 63, 87, 122, 
						145, 177, 209, 239, 265, 54, 80, 108, 139, 164, 197, 225};
						uint16_t letters_end[26] = {29, 82, 109, 137, 161, 188, 218, 241, 273, 304, 
						59, 83, 118, 141, 173, 205, 235, 261, 286, 76, 104, 135, 160, 193, 221, 265};						
						char chars[26] = {'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
						'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
						'Z', 'X', 'C', 'V', 'B', 'N', 'M'};
						
						for(uint8_t i = 0; i < 26; i++) {
							point start = {0, 0};
							point end = {0, 0};
							if(i <= 9) {
								start = (point){letters_str[i], 68};
								end = (point){letters_end[i], 106};
							} else if(i > 9 && i <= 18) {
								start = (point){letters_str[i], 110};
								end = (point){letters_end[i], 148};
							} else if(i > 18 && i <= 25) {
								start = (point){letters_str[i], 152};
								end = (point){letters_end[i], 190};
							}
							if(isPointInRange(&screen[5], &start, &end)) {
								Menu_Keyboard_AddNewChar(chars[i]);
							}
						}
					}*/
				} else if(mKeyboardDisplay == LOWERCASE) {
					if(isPointInRange(&screen[5], &(point){4, 159}, &(point){32, 190})) {
						mKeyboardDisplay = UPPERCASE;
						Menu_Keyboard_Refresh();
					} else if(isPointInRange(&screen[5], &(point){4, 208}, &(point){32, 233})) {
						mKeyboardDisplay = NUM;
						Menu_Keyboard_Refresh();
					} /*else if(isPointInRange(&screen[5], &(point){260, 159}, &(point){311, 181})) {
						Menu_Keyboard_RemoveLastChar();
					} else if(isPointInRange(&screen[5], &(point){270, 199}, &(point){311, 227})) {
						// list of most used entries
					} else if(isPointInRange(&screen[5], &(point){103, 194}, &(point){203, 235})) {
						Menu_Keyboard_AddNewChar(' ');
					} else {
						uint16_t letters_str[26] = {31, 56, 93, 117, 141, 159, 187, 212, 233, 258, 51, 80, 101, 
						131, 153, 178, 205, 223, 250, 64, 87, 115, 139, 162, 192, 217};
						uint16_t letters_end[26] = {52, 89, 113, 137, 155, 183, 208, 229, 254, 288, 76, 97, 127,
						149, 174, 201, 219, 246, 268, 83, 111, 135, 158, 188, 213, 255};
						char chars[26] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f',
						'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
						
						for(uint8_t i = 0; i < 26; i++) {
							point start = {0, 0};
							point end = {0, 0};
							if(i <= 9) {
								start = (point){letters_str[i], 68};
								end = (point){letters_end[i], 106};
							} else if(i > 9 && i <= 18) {
								start = (point){letters_str[i], 110};
								end = (point){letters_end[i], 148};
							} else if(i > 18 && i <= 25) {
								start = (point){letters_str[i], 152};
								end = (point){letters_end[i], 190};
							}
							if(isPointInRange(&screen[5], &start, &end)) {
								Menu_Keyboard_AddNewChar(chars[i]);
							}
						}
					}*/
				} else if(mKeyboardDisplay == NUM) {
					if(isPointInRange(&screen[5], &(point){4, 208}, &(point){32, 233})) {
						mKeyboardDisplay = LOWERCASE;
						Menu_Keyboard_Refresh();
					} else if(isPointInRange(&screen[5], &(point){260, 159}, &(point){311, 181})) {
						Menu_Keyboard_RemoveLastChar();
					} else if(isPointInRange(&screen[5], &(point){270, 199}, &(point){311, 227})) {
						// list of most used entries
					} else if(isPointInRange(&screen[5], &(point){103, 194}, &(point){203, 235})) {
						Menu_Keyboard_AddNewChar(' ');
					} else {
						uint16_t letters_str[26] = {29, 53, 83, 108, 138, 163, 191, 217, 244, 272,
						35, 71, 103, 129, 166, 225, 249, 271,
						91, 114, 133, 152, 167, 186, 207};
						uint16_t letters_end[26] = {50, 78, 103, 133, 159, 187, 212, 240, 267, 294,
						67, 98, 124, 161, 187, 245, 266, 287,
						129, 148, 163, 182, 203, 227};
						char chars[26] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
						'@', '#', '%', '^', '&', '*', '(', ')',
						 '!', '"', 39, ':', ';', '/', '?'};
						
						for(uint8_t i = 0; i < 26; i++) {
							point start = {0, 0};
							point end = {0, 0};
							if(i <= 9) {
								start = (point){letters_str[i], 68};
								end = (point){letters_end[i], 106};
							} else if(i > 9 && i <= 18) {
								start = (point){letters_str[i], 110};
								end = (point){letters_end[i], 148};
							} else if(i > 18 && i <= 25) {
								start = (point){letters_str[i], 152};
								end = (point){letters_end[i], 190};
							}
							if(isPointInRange(&screen[5], &start, &end)) {
								Menu_Keyboard_AddNewChar(chars[i]);
							}
						}
					}		
				}	
			} else if(gMenu == MENU_CALENDAR_ADDEVENT) {
				if(isPointInRange(&screen[5], &(point){181, 5}, &(point){319, 43})) { // event name
					SSD1289_Clear();
					mKeyboardString = mCalendarEventName;
					mCalendarEventName[0] = 0;
					mKeyboardDisplay = NUM;
					Menu_Keyboard_Refresh();
					gMenu = MENU_KEYBOARD_EVENT;
					mCalendarEvent = EVENT_NAME;
				} else if(isPointInRange(&screen[5], &(point){117, 60}, &(point){159, 95})) { // event hour
					SSD1289_Clear();
					mKeyboardString = mCalendarEventTemp;
					mCalendarEventTemp[0] = 0;
					mKeyboardDisplay = NUM;
					Menu_Keyboard_Refresh();
					gMenu = MENU_KEYBOARD_EVENT;
					mCalendarEvent = EVENT_HOUR;
				} else if(isPointInRange(&screen[5], &(point){175, 60}, &(point){215, 95})) { // event minutes
					SSD1289_Clear();
					mKeyboardString = mCalendarEventTemp;
					mCalendarEventTemp[0] = 0;
					mKeyboardDisplay = NUM;
					Menu_Keyboard_Refresh();
					gMenu = MENU_KEYBOARD_EVENT;
					mCalendarEvent = EVENT_MINUTES;
				} else if(isPointInRange(&screen[5], &(point){71, 110}, &(point){111, 145})) { // event date
					SSD1289_Clear();
					mKeyboardString = mCalendarEventTemp;
					mCalendarEventTemp[0] = 0;
					mKeyboardDisplay = NUM;
					Menu_Keyboard_Refresh();
					gMenu = MENU_KEYBOARD_EVENT;
					mCalendarEvent = EVENT_DAY;
				} else if(isPointInRange(&screen[5], &(point){127, 110}, &(point){167, 145})) { // event month
					SSD1289_Clear();
					mKeyboardString = mCalendarEventTemp;
					mCalendarEventTemp[0] = 0;
					mKeyboardDisplay = NUM;
					Menu_Keyboard_Refresh();
					gMenu = MENU_KEYBOARD_EVENT;
					mCalendarEvent = EVENT_MONTH;
				} else if(isPointInRange(&screen[5], &(point){183, 110}, &(point){255, 145})) { // event year
					SSD1289_Clear();
					mKeyboardString = mCalendarEventTemp;
					mCalendarEventTemp[0] = 0;
					mKeyboardDisplay = NUM;
					Menu_Keyboard_Refresh();
					gMenu = MENU_KEYBOARD_EVENT;
					mCalendarEvent = EVENT_YEAR;
				} else if(isPointInRange(&screen[5], &(point){204, 190}, &(point){299, 227})) { // save button
					Menu_Calendar_NewEvent_Save();
				}
			}
			gGesture = GESTURE_NONE;
			EIMSK |= (1 << INT1);
		} else if(gGesture == GESTURE_UP) {
			if(gMenu == MENU_MP3_ARTISTS) {
				if(mArtistsCurrentPage >= 1) {
					mArtistsCurrentPage--;
					Menu_MP3_Artists_Refresh();
				 } else {
					mArtistsCurrentPage = 0;
				 }					
			} else if(gMenu == MENU_MP3_SONGS) {
				if(mSongsCurrentPage >= 1) {
					mSongsCurrentPage--;
					Menu_MP3_Songs_Refresh();
				} else {
					mSongsCurrentPage = 0;
				}
			} else if(gMenu == MENU_RADIO_STATIONS) {
				if(mRStationsCurrentPage >= 1) {
					mRStationsCurrentPage--;
					Menu_Radio_Stations_Refresh();
				} else {
					mRStationsCurrentPage = 0;
				}
			} else if(gMenu == MENU_CALENDAR) {
				if(mCalendarMonthToDisplay >= 2) {
					mCalendarMonthToDisplay--;
					Menu_Calendar_Refresh();
				}
			}					
			gGesture = GESTURE_NONE;
			EIMSK |= (1 << INT1);
		} else if(gGesture == GESTURE_DOWN) {
			if(gMenu == MENU_MP3_ARTISTS) {
				if(mArtistsCurrentPage <= 254) {
					mArtistsCurrentPage++;
					Menu_MP3_Artists_Refresh();
				}				
			} else if(gMenu == MENU_MP3_SONGS) {
				if(mSongsCurrentPage <= 254) {
					mSongsCurrentPage++;
					Menu_MP3_Songs_Refresh();
				}
			} else if(gMenu == MENU_RADIO_STATIONS) {
				if(mRStationsCurrentPage <= 254) {
					mRStationsCurrentPage++;
					Menu_Radio_Stations_Refresh();
				}
			} else if(gMenu == MENU_CALENDAR) {
				if(mCalendarMonthToDisplay <= 11) {
					mCalendarMonthToDisplay++;
					Menu_Calendar_Refresh();
				}
			}
			gGesture = GESTURE_NONE;
			EIMSK |= (1 << INT1);
		} else if(gGesture == GESTURE_LEFT) {
			if(gMenu == MENU_HOME) {
				Menu_Main_Enter();
			}
			gGesture = GESTURE_NONE;
			EIMSK |= (1 << INT1);
		} else if(gGesture == GESTURE_RIGHT) {
			if(gMenu == MENU_MAIN) {
				Menu_Home_Enter();
			} else if(gMenu == MENU_MP3_ARTISTS || gMenu == MENU_RADIO_STATIONS || gMenu == MENU_CALENDAR/*|| gMenu == MENU_PHOTO_ALBUMS*/) {
				Menu_Main_Enter();
			} else if(gMenu == MENU_MP3_SONGS) {
				Menu_MP3_Artists_Refresh();
				mSongsCurrentPage = 0;
			} else if(gMenu == MENU_MP3_PLAYER) {
				if(mPlayerGoBackToMain)
					Menu_Main_Enter();
				else
					Menu_MP3_Songs_Refresh();
			} else if(gMenu == MENU_KEYBOARD_EVENT) {
				switch(mCalendarEvent) {
					case EVENT_NAME:
					break;
				
					case EVENT_HOUR:
					sscanf(mCalendarEventTemp, "%u", (uint16_t*)&mCalendarSelectedHour);
					break;
					
					case EVENT_MINUTES:
					sscanf(mCalendarEventTemp, "%u", (uint16_t*)&mCalendarSelectedMinutes);
					break;
					
					case EVENT_DAY:
					sscanf(mCalendarEventTemp, "%u", (uint16_t*)&mCalendarSelectedDay);
					break;
					
					case EVENT_MONTH:
					sscanf(mCalendarEventTemp, "%u", (uint16_t*)&mCalendarSelectedMonth);
					break;
					
					case EVENT_YEAR:
					sscanf(mCalendarEventTemp, "%u", (uint16_t*)&mCalendarSelectedYear);
					break;
				}
				Menu_Calendar_NewEvent_Add();
			} else if(gMenu == MENU_CALENDAR_ADDEVENT) {
				Menu_Calendar_Refresh();
			}
			gGesture = GESTURE_NONE;
			EIMSK |= (1 << INT1);
		} else if(gGesture == 0) { // bad touch hurts entire execution time
			gGesture = GESTURE_NONE;
			EIMSK |= (1 << INT1);
		}
	}
}	

#ifndef ATMEGA
ISR(PORTC_INT1_vect) { // RTC interrupt
	
}

ISR(PORTE_INT0_vect) { // VS1003 interrupt (DREQ went high), high priority
}

ISR(DMA_CH0_vect) {
	// read to first buffer
}

ISR(DMA_CH1_vect) {
	// read to second buffer
}
#endif

#ifdef ATMEGA
ISR(INT1_vect) {
	for(uint8_t i = 0; i < 128; i++) { // little debouncing
		if(bit_is_set(ADS7843_INT_PIN, ADS7843_INT_BIT))
			return;
	}
	if(gStatus & TOUCHSCREEN_IDLE && gGesture == GESTURE_NONE) {
		TCCR0B = (1 << CS00)|(1 << CS02); // start timer, 1024 prescaler
		gStatus &= ~TOUCHSCREEN_IDLE; // remove idle flag
		gStatus |= TOUCHSCREEN_READ; // add read flag
		EIMSK &= ~(1 << INT1); // disable this interrupt
	}
}

ISR(TIMER0_COMPA_vect) { // gesture detection interrupt
	if(gStatus & TOUCHSCREEN_IDLE && gGesture == GESTURE_NONE) {
		if(skippedSamples >= 5) {
			gStatus &= ~TOUCHSCREEN_IDLE; // remove idle flag
			gStatus |= TOUCHSCREEN_READ; // add read flag
		} else {
			skippedSamples++;
		}	
	}
}

ISR(TIMER1_COMPA_vect) { // RTC
	gRTC.seconds++;
	gRTCStatus = RTC_SECOND;
	if(gRTC.seconds >= 60) {
		gRTC.seconds = 0;
		gRTC.minutes++;
		gRTCStatus = RTC_MINUTE;
		if(gRTC.minutes >= 60) {
			gRTC.minutes = 0;
			gRTC.hours++;
			gRTCStatus = RTC_HOUR;
			if(gRTC.hours >= 23) {
				gRTCStatus = RTC_READ;
			}
		}
	}
}

#endif